﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class UserDAL
    {
        public static int Save(UserInfo info)
        {
            string sql = string.Format("insert into [user]([UserName],[UserPwd],[RealName],[Mobile],[HeadImg],[Sex],[Enable],[Token],[CreateDate]) values(@UserName,@UserPwd,@RealName,@Mobile,@HeadImg,@Sex,@Enable,@Token,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@Sex", info.Sex),
                new SqlParameter("@Enable", info.Enable),
                new SqlParameter("@Token", info.Token),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(UserInfo info)
        {
            string sql = string.Format("update [user] set [UserName]=@UserName,[UserPwd]=@UserPwd,[RealName]=@RealName,[Mobile]=@Mobile,[HeadImg]=@HeadImg,[Sex]=@Sex,[Enable]=@Enable,[Token]=@Token where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@Sex", info.Sex),
                new SqlParameter("@Enable", info.Enable),
                new SqlParameter("@Token", info.Token),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static UserInfo Login(string username, string userpwd)
        {
            SqlParameter[] sp = new SqlParameter[2];
            sp[0] = new SqlParameter("@UserName", username);
            sp[1] = new SqlParameter("@UserPwd", userpwd);
            DataRow dr = CommonDAL.GetDataRow("user_login", sp);
            return SimpleData(dr);
        }
        public static UserInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[user]");
            return SimpleData(dr);
        }
        public static UserInfo FindByOpenID(string openid)
        {
            DataRow dr = CommonDAL.GetDataRow(string.Format("select * from [user] where OpenID='{0}'", openid));
            return SimpleData(dr);
        }
        public static bool UpdateRole(int id, float money)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [user] set Money=Money+{0},Role=2 where ID={1}", money, id));
        }
        public static List<UserInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable("select top 10 * from [user]");
            return ListSimpleData(dt);
        }
        public static List<UserInfo> FindListByIDs(string ids)
        {
            ids = ids.Replace(",,,", ",").Replace(",,", ",");
            DataTable dt = CommonDAL.FindListByIDs(ids, "[user]");
            return ListSimpleData(dt);
        }
        public static bool CheckMobile(string mobile)
        {
            int count = DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [user] where Mobile='{0}'", mobile)));
            return count > 0 ? true : false;
        }
        public static int CheckToken(string token)
        {
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select ID from [user] where Token='{0}'", token)));
        }
        public static bool SmsCode(string mobile, string smscode)
        {
            string code = DataConvert.ToString(CommonDAL.FindColumnByColumn("smscode", "mobile", mobile, "smscode"));
            if (code == "")
            {
                return CommonDAL.ExecuteNoneQuery(string.Format("insert into smscode(mobile,smscode) values('{0}','{1}')", mobile, smscode));
            }
            else
            {
                return CommonDAL.ExecuteNoneQuery(string.Format("update smscode set smscode='{0}' where mobile='{1}'", smscode, mobile));
            }
        }
        public static List<UserInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and ([user].UserName like '%{0}%' or [user].Mobile like '%{0}%')", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [user].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[user]", out count);
            return ListSimpleData(dt);
        }
        private static List<UserInfo> ListSimpleData(DataTable dt)
        {
            List<UserInfo> list = new List<UserInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<UserInfo> ListAllData(DataTable dt)
        {
            List<UserInfo> list = new List<UserInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static UserInfo SimpleData(DataRow dr)
        {
            UserInfo info = new UserInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.UserPwd = DESEncrypt.Decrypt(DataConvert.ToString(dr["UserPwd"]));
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.HeadImg = DataConvert.ToString(dr["HeadImg"]);
                info.Sex = DataConvert.ToString(dr["Sex"]);
                info.Enable = DataConvert.ToInt(dr["Enable"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

            }
            return info;
        }
        private static UserInfo AllData(DataRow dr)
        {
            UserInfo info = new UserInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.UserPwd = DataConvert.ToString(dr["UserPwd"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.HeadImg = DataConvert.ToString(dr["HeadImg"]);
                info.Sex = DataConvert.ToString(dr["Sex"]);
                info.Enable = DataConvert.ToInt(dr["Enable"]);
                info.Token = DataConvert.ToString(dr["Token"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }



    }
}
